/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testing.jee.web.view;

import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.event.dd.acceptcriteria.ServerSideCriterion;
import com.vaadin.ui.DragAndDropWrapper;
import java.util.logging.Level;
import java.util.logging.Logger;
import testing.jee.entity.Workpackage;

/**
 *
 * @author kreisera
 */
public class MergeDropHandler implements DropHandler {

    final long assocId;
    final long targetId;
    final AssignWorkpackageView view;

    public MergeDropHandler(long assocId, long targetId, AssignWorkpackageView view) {
        this.assocId = assocId;
        this.targetId = targetId;
        this.view = view;
    }
    
    @Override
    public void drop(DragAndDropEvent event) {
        try {
            Workpackage sourceWorkpackage = (Workpackage) ((DragAndDropWrapper)event.getTransferable().getSourceComponent()).getData();
            view.req.mergeWorkpackages(sourceWorkpackage.getId(), targetId);
            view.createWorkpackagePanels();
        } catch (Exception ex) {
            Logger.getLogger(MergeDropHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public AcceptCriterion getAcceptCriterion() {
        ServerSideCriterion criterion = new ServerSideCriterion() {

            @Override
            public boolean accept(DragAndDropEvent event) {
                Workpackage sourceWorkpackage = (Workpackage) ((DragAndDropWrapper)event.getTransferable().getSourceComponent()).getData();
                if (sourceWorkpackage.getId() != targetId && sourceWorkpackage.getAssocId() == assocId)
                    return true;
                return false;
            }
        };
        return criterion;
    }
}
